COPYRIGHT = Copyright (c) 2011-2012,2018-2022,2024, the ACME Software Deli

# ============================================================================
#   This program is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
#
#   Permission to use for any purpose, modify, copy, and make enhancements
#   and derivative works of the software is granted if attribution is given to
#   R.M. Gillmore, dba the ACME Software Deli, as the author
#
#   While the ACME Software Deli does not work for money, there is nonetheless a
#   a significant amount of work involved.  The ACME Software Deli maintains the
#   rights to all code written, though it may be used and distributed as long as
#   the following conditions are maintained.
#
#   1.  The copyright statement at the top of each code block is maintained in
#       your distribution.
#   2.  You do not identify yourself as the ACME Software Deli
#   3.  Any changes made to the software are sent to the ACME Software Deli
#   4.  You distribute your code and any modifications to my code freely.
#   5.  There is no charge for your work which uses this library
# ============================================================================

#
# @(#) $Id: Makefile 942 2024-03-03 19:57:23Z rmgillmore $
#

# _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_
#	definitions which allow the Makefile to work with 32-bit and 64-bit
#	versions of Windows, and to allow this Makefile to work regardless of the
#	location of this file on the disk

SHELL = cmd.exe
# OSARCH = $(shell osArch.cmd)
MAKEFILE = Makefile
MAKEFILE_DEPENDS = $(MAKEFILE).dep
MAKEFILE_SOURCES = $(MAKEFILE).sources
FLAGFILE = $(MAKEFILE).flg
:WORK: = C:\Users\Owner\Documents\plm86
LOCAL_UTILS = $(shell back2front $(UTILS))
INTEL_TOOLS = $(LOCAL_UTILS)/plm86Tools
MS4TOOLS = $(HOME)/Documents/utils/masm4
MSTOOLS = $(HOME)/Documents/utils/masm510/bin
SUPPORT_DIR = $(LOCAL_UTILS)/source
TEST_DIR = TestApps
EIGHT_DOT_3_SCRIPT = 8dot3path.cmd

SHORT_UTILS = $(shell $(EIGHT_DOT_3_SCRIPT) $(UTILS))
PLM86_TOOLS = $(shell $(EIGHT_DOT_3_SCRIPT) $(INTEL_TOOLS))
MASM4_TOOLS = $(shell $(EIGHT_DOT_3_SCRIPT) $(MS4TOOLS))
MASM5_TOOLS = $(shell $(EIGHT_DOT_3_SCRIPT) $(MSTOOLS))
HOME_8DOT3 = $(shell $(EIGHT_DOT_3_SCRIPT) $(HOMEPATH)\Documents)
REM_SUBSTR = $(SUPPORT_DIR)/rmSubStr.exe
CWD = $(shell pwd)
CWD_8DOT3 = $(shell $(EIGHT_DOT_3_SCRIPT) $(CWD))
APPS_8DOT3 = $(shell $(EIGHT_DOT_3_SCRIPT) $(shell parentDir.cmd) )\Apps
CURRENT_8DOT3 = $(HOMEDRIVE)$(shell $(REM_SUBSTR) $(CWD_8DOT3) $(HOME_8DOT3))
UTILS_8DOT3 = $(HOMEDRIVE)$(shell $(REM_SUBSTR) $(SHORT_UTILS) $(HOME_8DOT3))
PLM86T_8DOT3 = $(HOMEDRIVE)$(shell $(REM_SUBSTR) $(PLM86_TOOLS) $(HOME_8DOT3))
MASM4T_8DOT3 = $(HOMEDRIVE)$(shell $(REM_SUBSTR) $(MASM4_TOOLS) $(HOME_8DOT3))
MASMT_8DOT3 = $(HOMEDRIVE)$(shell $(REM_SUBSTR) $(MASM5_TOOLS) $(HOME_8DOT3))
DOS_APPS_8DOT3 = $(HOMEDRIVE)$(shell $(REM_SUBSTR) $(APPS_8DOT3) $(HOME_8DOT3))
DOS_PATH = $(UTILS_8DOT3);$(PLM86T_8DOT3);$(MASM4T_8DOT3);$(MASMT_8DOT3)

ECHO = echo
ACME_OBJ = obj
ARCH = exe
INCLUDE = .

DEL = del /f /q
MOVE = ren
PACK = exepack
PACKFILE = exePakd
# DOS = dosbox -noconsole -c
DOS = vDos
DOS_INIT_FILE = autoexec.txt
DOS_CONFIG_FILE = config.txt
DOS_SCRIPT = vDosWork.bat

# LINK = link16
# LINK = link16 /codeview /linenumbers
# LINK = $(MSTOOLS)/link16 /noignorecase /exepack /codeview /linenumbers /warnfixup
# LINK = $(MSTOOLS)/link16 /noignorecase /codeview /linenumbers /warnfixup /cparm:1
# LINK = link16 /noignorecase /codeview /linenumbers /warnfixup

# ASM = $(MSTOOLS)/masm /Zi
# ASM = masm /Zi

# LIB = lib16
# LIB = tlib /C

# _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_
#	if we want to distribute the executables without the "what" strings,
#	run make as:
#		make clean AddWhat=1 all

ifdef AddWhat
	noIDString=set(noID=0)
	asmIdString=
else
	noIDString = set(noID)
	asmIdString = /DSMALL
endif

# _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_
#	if we want to distribute the executables with Logging enabled, run make as:
#		make clean Logging=1 all

ifdef Logging
	doLogging = set(LOGGING)
else
	doLogging=
endif

# _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_
#
#	when building a development version of the apps, run make as:
#		make clean all
#	this turns on debugging symbols, and logging, but no ID strings
#
#	when building a release version of the library, run make as:
#		make DoRelease=1 clean all
#	this turns off debugging symbols, and turns on ID strings.  Logging is
#	always included as an option so that the user may request it
#
# _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_

ifdef DoRelease
	ASM = masm /DSMALL
	DEBUG_FLAG = set(noID=1) $(doLogging)
	LINK = link16 /packcode /packdata /map
else
	ASM = masm /Zi
	DEBUG_FLAG = debug set(noID=0) set(LOGGING)
	LINK = link16 /packcode /packdata /map /codeview /linenumbers
endif

PREPROC = python ..\plmPreProc.py
PARSEOUT = python $(SUPPORT_DIR)/parseOutput.py

PLM_LIBRARY =		..\\lib\\plm86.lib
START_OBJ = 		..\lib\asmStart.$(ACME_OBJ)
SHORT_START_OBJ =	..\lib\shrtStrt.$(ACME_OBJ)
ACME_LIBRARY =		$(START_OBJ) $(SHORT_START_OBJ) $(PLM_LIBRARY)

# _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_
#	the files listed here compose those used to test concepts or library
#	functionality

TEST_EXECS = \
	tArrayMg.$(ARCH) \
	tcmdArgs.$(ARCH) \
	tdate.$(ARCH) \
	tdirHlpr.$(ARCH) \
	tdrHlpr2.$(ARCH) \
	tDiskRd.$(ARCH) \
	tDOSMem.$(ARCH) \
	tenvArg.$(ARCH) \
	testScrl.$(ARCH) \
	tfgets.$(ARCH) \
	tfileIO.$(ARCH) \
	thstName.$(ARCH) \
	tiniMgr.$(ARCH) \
	tInsert.$(ARCH) \
	tkbhit.$(ARCH) \
	tkbhit.$(ARCH) \
	tldExec.$(ARCH) \
	tlog.$(ARCH) \
	tMem.$(ARCH) \
	tNameStr.$(ARCH) \
	tnumStrs.$(ARCH) \
	tposixDr.$(ARCH) \
	tptrMath.$(ARCH) \
	tstrtok.$(ARCH) \
	tuname.$(ARCH) \
	tWinMgr.$(ARCH)	\
	vidTest.$(ARCH) \

#	tAcMem.$(ARCH) \
#	tparamD.$(ARCH) \

ALL_EXECS = \
	$(TEST_EXECS) \

# _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_
#	filename suffixes listed and instructions for each

.SUFFIXES : .$(ACME_OBJ) .p86 .ext .a86 .e86 .$(ARCH) .sym

.a86.$(ACME_OBJ) :
	@echo -- Assembling $<
	@if not exist $(DOS_INIT_FILE) make $(DOS_INIT_FILE)
	@if not exist $(DOS_CONFIG_FILE) make $(DOS_CONFIG_FILE)
	@echo @echo off > $(DOS_SCRIPT)
	@echo $(ASM) $<,,; >> $(DOS_SCRIPT)
ifeq ($(OSARCH), 64)
	@echo exit 0 >> $(DOS_SCRIPT)
	@$(DOS)
else
	@cmd /c $(DOS_SCRIPT) 1>temp 2>&1
endif
	@if exist temp $(DEL) temp
	@$(MOVE) $(*F).lst temp
	@$(MOVE) temp $(*F).lst
	@$(DEL) $(DOS_SCRIPT)
	@$(MOVE) $@ temp
	@$(MOVE) temp $@

.p86.$(ACME_OBJ) :
	@echo -- Compiling $<
	@if not exist $(DOS_INIT_FILE) make $(DOS_INIT_FILE)
	@if not exist $(DOS_CONFIG_FILE) make $(DOS_CONFIG_FILE)
	@$(PREPROC) -of $(*F).ppp -if $< > NUL:
	@$(DEL) plmPreProc*log
	@echo @echo off > $(DOS_SCRIPT)
	@echo plm86 $(*F).ppp $(DEBUG_FLAG) >> $(DOS_SCRIPT)
ifeq ($(OSARCH), 64)
	@$(DOS)
else
	@$(DOS_SCRIPT) 1>temp 2>&1
endif
	@if exist $(*F).ppp $(DEL) $(*F).ppp
	@if exist temp $(DEL) temp
	@$(MOVE) $(*F).lst temp
	@$(MOVE) temp $(*F).lst
	@$(MOVE) $(*F).obj temp1
	@$(MOVE) temp1 $(*F).obj

.$(ACME_OBJ).$(ARCH) :
	@echo -= Linking $@
	@$(LINK) $(START_OBJ) $<,$@,/map,$(PLM_LIBRARY) ; > temp
	@if exist temp $(DEL) temp
	@$(MOVE) $@ temp
	@$(MOVE) temp $@
	@echo == Debugging Symbols: $@
	@echo @echo off > $(DOS_SCRIPT)
	@echo mapsym $(*F).map $(*F).sym >> $(DOS_SCRIPT)
ifeq ($(OSARCH), 64)
	@$(DOS)
else
	@cmd /c $(DOS_SCRIPT)
endif
	@$(DEL) $(DOS_SCRIPT)
	@if exist temp $(DEL) temp
	@$(MOVE) $(*F).sym temp
	@$(MOVE) temp $(*F).sym
	@if exist temp $(DEL) temp
	@$(MOVE) $@ temp
	@$(MOVE) temp $@

.PHONY : all apps lib clean depend repenv test

# _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_
#	these are the actual target specifications

all : test

test : $(MAKEFILE_SOURCES) \
		$(DOS_INIT_FILE) \
		$(DOS_CONFIG_FILE) \
		$(TEST_EXECS)

lib.rsp : $(LIB_OBJS)
	@objsToRsp $(LIB_OBJS) > $@

clean :
#	@$(DEL) *.rsp
	@if exist *.sym $(DEL) *.sym
#	@$(DEL) *.tr
#	@$(DEL) *.map
	@if exist *.$(ACME_OBJ) $(DEL) *.$(ACME_OBJ)
	@if exist temp $(DEL) temp
	@if exist $(DOS_INIT_FILE) $(DEL) $(DOS_INIT_FILE)
	@if exist $(DOS_CONFIG_FILE) $(DEL) $(DOS_CONFIG_FILE)
	@if exist $(8_DOT_3_SCRIPT) $(DEL) $(8_DOT_3_SCRIPT)
	@if exist $(FLAGFILE) $(DEL) $(FLAGFILE)
	@if exist $(DOS_SCRIPT) $(DEL) $(DOS_SCRIPT)
	@cleanup -n -f

repenv :
	@env.exe

report :
	@echo home 8.3 $(HOME_8DOT3)
	@echo cwd 8.3 $(CWD_8DOT3)
	@echo current 8.3 $(CURRENT_8DOT3)
	@echo utils 8.3 $(UTILS_8DOT3)
	@echo plmtools 8.3 $(PLM86T_8DOT3)
	@echo masmtools 8.3 $(MASMT_8DOT3)
	@echo appsDir 8.3 $(APPS_8DOT3)
	@echo dosApps 8.3 $(DOS_APPS_8DOT3)

# _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_
#	specific file dependencies defined

$(8_DOT_3_SCRIPT) : $(MAKEFILE)
	echo @ECHO OFF> $@
	echo if '%1'=='' (%0 .) else echo %~s1>> $@

$(DOS_CONFIG_FILE) : $(MAKEFILE)
	@echo Creating $@
	@echo TEXT = 35x95> $@
	@echo FONT = C:\WINDOWS\FONTS\CONSOLA>> $@
#	@echo FILES = 50>> $@

$(DOS_INIT_FILE) : $(MAKEFILE)
	@set TEMP_STR =
	@echo Creating $@
	@echo @echo off> $@
	@echo rem>> $@
	@echo rem	mount the home directory as C, and setup a path>> $@
	@echo rem>> $@
	@echo.>> $@
	@echo use D: C:\vDos>> $@
	@echo use C: $(HOME_8DOT3)>> $@
	@echo set path=$(HOMEDRIVE)\;$(DOS_PATH)>> $@
	@echo set path=$(DOS_APPS_8DOT3);%%path%%>> $@
	@echo.>> $@
	@echo rem>> $@
	@echo rem	change to the directory where we need some work done, and get to it>> $@
	@echo rem>> $@
	@echo.>> $@
	@echo cd $(CURRENT_8DOT3)>> $@
	@echo if exist $(DOS_SCRIPT) call $(DOS_SCRIPT) >> $@
	@echo if exist $(DOS_SCRIPT) exit ERRORLEVEL >> $@
	@echo.>> $@
	@echo rem>> $@
	@echo rem	when you get this far, we will have a command prompt,>> $@
	@echo rem	so make it easier to read>> $@
	@echo rem>> $@
	@echo.>> $@
	@echo dosColor -n >> $@

$(FLAGFILE) : $(MAKEFILE)
	@touch $@ > nul:

depend $(MAKEFILE_DEPENDS) : $(MAKEFILE)
	@python $(SUPPORT_DIR)/mkDepend.py -nl -ic -sd . -fs "*.?86;" -id "$(INCLUDE)" -of $(MAKEFILE_DEPENDS)
	@touch $(FLAGFILE) > nul:

$(MAKEFILE_SOURCES) : $(MAKEFILE) \
		$(SUPPORT_DIR)/echoEach.awk $(SUPPORT_DIR)/objectToSource.awk
	@$(ECHO) $(ALL_EXECS) | gawk -f $(SUPPORT_DIR)/echoEach.awk > $(MAKEFILE).exeList
	@gawk -f $(SUPPORT_DIR)/objectToSource.awk VERBOSE=-1 OBJEXT=$(ACME_OBJ) EXEEXT=$(ARCH) LIBNAME=$(PLM_LIBRARY) $(MAKEFILE).exeList > $(MAKEFILE_SOURCES)
	@$(DEL) $(MAKEFILE).exeList
	sleep 2

##################
# _-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_-^-_
#	included are dependency definitions for the source and executable files
#	they are included because they are generated.  they are then free to
#	change and will not cause the entire package to be built again

iCPUid.$(ARCH) : iCPUid.$(ACME_OBJ)
	@echo _= Linking $@
	@$(LINK) $<,$@,/map,;

mornBob.com	: mornBob.$(ACME_OBJ)
	@echo -= Linking $@
	@$(LINK) /tiny $<,$@,/map ; > temp
	@if exist temp $(DEL) temp
	@$(MOVE) $@ temp
	@$(MOVE) temp $@
	@echo == Creating Symbols File for Debugging $@
	@echo @echo off > $(DOS_SCRIPT)
	@echo mapsym $(*F).map $(*F).sym >> $(DOS_SCRIPT)
ifeq ($(OSARCH), 64)
	@echo exit 0 >> $(DOS_SCRIPT)
	@$(DOS)
else
	@$(DOS_SCRIPT) > temp
endif
	@$(DEL) $(DOS_SCRIPT)
	@if exist temp $(DEL) temp
	@$(MOVE) $(*F).sym temp
	@$(MOVE) temp $(*F).sym
	@$(MOVE) $@ temp
	@$(MOVE) temp $@

##################
include $(MAKEFILE_DEPENDS)
include $(MAKEFILE_SOURCES)
